//
//  JourneyPhotoViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-27.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyPhotoViewController.h"
#import "JourneryNetHandler.h"
#import "JourneyPhotoCollectionViewCell.h"
#import "PhotoWallCollectionViewCell.h"
#import "SDRefreshStart.h"
#import "JourneyWayPointsViewController.h"

@interface JourneyPhotoViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,SDRefreshStartDelegate>
@property (nonatomic , retain)UICollectionView *collectionView;
@property (nonatomic , retain)NSMutableArray *photoArr;
@property (nonatomic , retain)UICollectionView *photoCollectionView;
@property (nonatomic , assign)NSInteger count;
@end

@implementation JourneyPhotoViewController

-(void)dealloc
{
    [_collectionView release];
    [_photoCollectionView release];
    [_photoArr release];
    [_photoData release];
    [_idNumber release];
    [_type release];
    [super dealloc];
    
}

- (void)netHandler
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18", self.type,self.idNumber];
    NSLog(@"!!!!!!!!!!!!!!!!%@",str);
    
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        
        
        NSMutableArray *resultArr = [data objectForKey:@"items"];
        
        self.photoArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in resultArr) {
            JourneyPhotoDataSource *photoData = [[JourneyPhotoDataSource alloc]initWithDictionary:dic];
            
            [self.photoArr addObject:photoData];
            
            [photoData release];
            
        }
        [_collectionView reloadData];
        [_photoCollectionView reloadData];
    }];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    self.navigationController.navigationBar.translucent = NO;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    //    self.view.backgroundColor = [UIColor cyanColor];
    
    [self netHandler];
    
    CGRect photoFrame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64);
    CGRect photoFrame1 = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    UICollectionViewFlowLayout *flowLayout1 = [[UICollectionViewFlowLayout alloc]init];
    flowLayout1.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout1.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    //最小列间距
    flowLayout1.minimumInteritemSpacing = 0;
    //最小行间距
    flowLayout1.minimumLineSpacing = 0;
    //边缘距离
    flowLayout1.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.photoCollectionView = [[UICollectionView alloc]initWithFrame:photoFrame1 collectionViewLayout:flowLayout1];
    //    _photoCollectionView.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
    _photoCollectionView.userInteractionEnabled = YES;
    _photoCollectionView.pagingEnabled = YES;
    _photoCollectionView.delegate = self;
    _photoCollectionView.dataSource = self;
    [self.view addSubview:_photoCollectionView];
    [_photoCollectionView release];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(80, 80);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    //边缘距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 20, 20);
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:photoFrame collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
    _collectionView.userInteractionEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
#pragma mark 注册collecttionView
    //注册collecttionView
    [_collectionView registerClass:[JourneyPhotoCollectionViewCell class] forCellWithReuseIdentifier:@"旅行照片"];
    
    [_photoCollectionView registerClass:[PhotoWallCollectionViewCell class] forCellWithReuseIdentifier:@"旅行照片墙"];
    
    [SDRefreshStart SDRefeshTableView:self.collectionView delegate:self];
    [SDRefreshStart SDRefeshTableView:self.photoCollectionView delegate:self];
}

#pragma mark 返回按钮
- (void)backBtuAction:(UIButton *)btu
{
    [self.view bringSubviewToFront:_collectionView];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark UICollectionView delegate & datasource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [_photoArr count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _collectionView) {
        JourneyPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"旅行照片" forIndexPath:indexPath];
        
        cell.photoData = [self.photoArr objectAtIndex:indexPath.item];
        return cell;
        
    }else if (collectionView == _photoCollectionView)
    {
        PhotoWallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"旅行照片墙" forIndexPath:indexPath];
        //        cell.backgroundColor = [UIColor whiteColor];
        cell.photoData = [self.photoArr objectAtIndex:indexPath.item];
        cell.mode = PhotosCVCellModeMax;
#pragma mark block方法
        [cell setBlock:^{
            [self.view bringSubviewToFront:_collectionView];
            self.navigationController.navigationBarHidden = NO;
        }];
        [cell setCheckblock:^{
            JourneyWayPointsViewController *wayPointVC=  [[JourneyWayPointsViewController alloc]init];
            wayPointVC.target_id = self.idNumber;
            wayPointVC.target_type = self.type;
            wayPointVC.idNumber = cell.photoData.idNumber;
            [self.navigationController pushViewController:wayPointVC animated:NO];
            [wayPointVC release];
        }];
        return cell;
    }
    return nil;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _collectionView) {
        self.navigationController.navigationBarHidden = YES;
        [self.photoCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        [self.view bringSubviewToFront:_photoCollectionView];
        
    }
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == self.photoArr.count - 1) {
        [self footRefesh];
    }
}

#pragma mark - 下拉刷新
-(void)headRefesh
{
}
#pragma mark - 上拉加载
- (void)footRefesh
{
    self.count = self.count+18;
    
    NSString *cellCount = [NSString stringWithFormat:@"%ld",self.count];
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=%@", self.type,self.idNumber,cellCount];
    NSLog(@"!!!!!!!!!!!!!!!!%@",str);
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        
        
        NSMutableArray *resultArr = [data objectForKey:@"items"];
        
        self.photoArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in resultArr) {
            JourneyPhotoDataSource *photoData = [[JourneyPhotoDataSource alloc]initWithDictionary:dic];
            
            [self.photoArr addObject:photoData];
            
            [photoData release];
            
        }
        [_collectionView reloadData];
        [_photoCollectionView reloadData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = YES;
    [[UINavigationBar appearance] setTintColor:[UIColor colorWithWhite:0.0 alpha:0.3]];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}



- (void)viewDidDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
